package com.icity.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.icity.entity.Echartpie;
import com.icity.entity.visit.VisitAll;
import com.icity.entity.visit.VisitLaunchInfo;
import com.icity.mapper.VisitMapper;
import com.icity.service.VisitService;
import com.icity.util.DateUtils;
@Service
@Transactional
public class VisitServiceImpl implements VisitService {

	@Autowired
	private VisitMapper visitMapper;

	@Override
	public Echartpie getVisitLine(Map<Object, Object> map) {
		return null;
	}

	@Override
	public Map<Object, Object> getWeekDay(Map<Object, Object> mapDate, int days) {
		Map<Object, Object> map = new HashMap<Object, Object>();
		List<String> list = new ArrayList<String>();
		if (mapDate.containsKey("nowDate")) {
			String date = mapDate.get("nowDate").toString();
			for (int i = days; i >= 1; i--) {
				list.add(DateUtils.getSpecifiedDayBefore(date, i));
			}
		} else {
			days = (int) DateUtils.getDaySub(mapDate.get("startDate").toString(), mapDate.get("endDate").toString());
			for (int i = days; i >= 0; i--) {
				list.add(DateUtils.getSpecifiedDayBefore(mapDate.get("endDate").toString(), i));
			}
		}
		map.put("type", "category");
		map.put("boundaryGap", false);
		map.put("data", list);
		return map;
	}

	@Override
	public Map<Object, Object> getGrid() {
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("left", "3%");
		map.put("right", "4%");
		map.put("bottom", "3%");
		map.put("containLabel", true);
		return map;
	}

	@Override
	public Map<Object, List<String>> getLegend() {
		Map<Object, List<String>> map = new HashMap<Object, List<String>>();
		List<String> list = new ArrayList<String>();
		list.add("Android");
		list.add("Ios");
		list.add("Web");
		map.put("data", list);
		return map;
	}

	@Override
	public Map<Object, String> getTooltip() {
		Map<Object, String> map = new HashMap<Object, String>();
		map.put("trigger", "axis");
		return map;
	}

	@Override
	public Map<Object, Object> getYaxis() {
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("type", "value");
		return map;
	}

	@Override
	public List<Map<Object, Object>> getVisitSeries(Map<Object, Object> map) {
		System.out.println(map.get("startDate")+"---"+map.get("endDate"));
		List<VisitLaunchInfo> listVisit = visitMapper.getVisitNumList(map);
		List<Integer> listAndroid = new ArrayList<Integer>();
		List<Integer> listIos = new ArrayList<Integer>();
		List<Integer> listWeb = new ArrayList<Integer>();
		Map<Object, Object> mapAndroid = new HashMap<Object, Object>();
		Map<Object, Object> mapIos = new HashMap<Object, Object>();
		Map<Object, Object> mapWeb = new HashMap<Object, Object>();
		List<Map<Object, Object>> listSeries = new ArrayList<Map<Object, Object>>();
		for(VisitLaunchInfo visitLaunchInfo : listVisit){
			if(visitLaunchInfo.getType()==2){
				listAndroid.add(visitLaunchInfo.getValue());
			}else if(visitLaunchInfo.getType()==1){
				listIos.add(visitLaunchInfo.getValue());
			}else if(visitLaunchInfo.getType()==3){
				listWeb.add(visitLaunchInfo.getValue());
			}
		}
		mapAndroid.put("name", "Android");
		mapAndroid.put("type", "line");
		mapAndroid.put("stack", "总量");
		mapAndroid.put("data", listAndroid);

		mapIos.put("name", "Ios");
		mapIos.put("type", "line");
		mapIos.put("stack", "总量");
		mapIos.put("data", listIos);

		mapWeb.put("name", "Web");
		mapWeb.put("type", "line");
		mapWeb.put("stack", "总量");
		mapWeb.put("data", listWeb);

		listSeries.add(mapAndroid);
		listSeries.add(mapIos);
		listSeries.add(mapWeb);
		return listSeries;
	}

	@Override
	public VisitAll getAllVisit() {
		return visitMapper.getVisitAll();
	}

}
